package com.zhida.jiyangcenter.api

import chooongg.base.manager.app
import chooongg.base.utils.startActivity
import chooongg.base.utils.tipWarn
import chooongg.base.utils.withMain
import chooongg.http.APIException
import chooongg.http.HttpCallback
import com.zhida.jiyangcenter.account.LoginPassActivity
import com.zhida.jiyangcenter.mian.MainActivity
import kotlinx.coroutines.CoroutineScope

abstract class BaseAPI<T> : HttpCallback<NetResponse<T>>() {

    abstract fun onSuccess(t: T?, state: Int, msg: String)

    override suspend fun response(t: NetResponse<T>) {
        if (t.state != 0) {
            if (t.state==-100){
                withMain {
                    tipWarn("请重新登录")
                    for (i in app.activityStack.size - 1 downTo 0) {
                        if (app.activityStack[i] !is LoginPassActivity) {
                            app.activityStack[i].finish()
                        }
                    }
                    app.activityTop.startActivity<LoginPassActivity> {  }
                }
            }else {
                withMain {
                    onError(APIException(t.state.toString(), t.msg))
                }
            }
        } else {
            withMain {
                onSuccess(t.data, t.state, t.msg)
            }
        }
    }

}